package com.thinkmore.business.bean.result;

import com.thinkmore.business.constant.Constant;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class CommonResult {
    private String code;
    private String msg;
    private Object data;

    public CommonResult() {
        successInfo();
    }

    public CommonResult(Object data) {
        successInfo();
        this.data = data;
    }

    public CommonResult(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public CommonResult(String code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public void successInfo() {
        this.code = Constant.SUCCESS_CODE;
        this.msg = Constant.SUCCESS_MSG;
    }

    public static CommonResult errorMsg(String msg) {
        CommonResult result = new CommonResult(Constant.ERROR_CODE, msg);
        return result;
    }

    public static CommonResult businessError(String msg) {
        CommonResult result = new CommonResult(Constant.BUSINESS_ERROR_CODE, msg);
        return result;
    }

    public static CommonResult errorNullParam() {
        return errorMsg(Constant.MSG_NULLPARAM);
    }

    public boolean success() {
        return this.code != null && Constant.SUCCESS_CODE.equals(this.code);
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }
}
